<template>
	<view class="page">
		<view class="u-p-t-50">
			<u-swiper :list="list" :effect3d="true" height="1000" @change="changeSwiper" img-mode="scaleToFill" bg-color="#670000"></u-swiper>
		</view>
		<view class="position-fixed left-0 right-0 bottom-0 safe-bottom">
			<view class="flex align-center justify-evenly u-p-b-40">
				<view class="text-center rounded-35" style="width: 200rpx; height: 70rpx; background-color: #ffd800;"
					@click="save">
					<text class="font-28" style="line-height: 70rpx; color: #000;">保存本地</text>
				</view>
				<!-- #ifdef APP-PLUS -->
				<view class="text-center rounded-35" style="width: 200rpx; height: 70rpx; background-color: #ffd800;"
					@click="send">
					<text class="font-28" style="line-height: 70rpx; color: #000;">分享好友</text>
				</view>
				<!-- #endif -->
				<!-- #ifdef MP-WEIXIN -->

				<button class="text-center rounded-35 font-28"
					style="line-height: 70rpx; margin: 0; color: #000; width: 200rpx; height: 70rpx; background-color: #ffd800;"
					open-type="share">分享好友</button>
				<!-- #endif -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		MineCenterApi,
		BaseCenterApi
	} from '@/api/serves.js'
	export default {
		data() {
			return {
				share_image: '',
				current: 0,
				list: [],
			}
		},
		onLoad() {
			this._initData()
		},
		methods: {
			changeSwiper(current){
				this.current  = current;
			},
			_initData() {
				uni.showLoading({
					title: '海报制作中...'
				})
				MineCenterApi.getConfig().then(res => {
					console.log('获取配置', res);
					if (res.code == 1) {
						let result = res.data.data.share_image
						let iterable = []
						if (!!result.length) {
							result.forEach((item, index) => {
								iterable.push(BaseCenterApi.getActivityImg({
									type: index
								}))
							})
							Promise.all(iterable).then(res => {
								console.log('海报生成', res);
								uni.hideLoading()
								if (res.length) this.list = res.map(item => item.data)
							})
						}
					} else {
						this.$u.toast(res.msg)
						uni.hideLoading()
					}
				})
			},
			downloadFile() {
				uni.downloadFile({
					url: this.list[this.current],
					success: (res) => {
						console.log("保存图片的res",res);
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
										uni.hideLoading()
									uni.showToast({
										title: "保存成功",
										icon: "none"
									});
								},
								fail: function() {
										uni.hideLoading()
									uni.showToast({
										title: "保存失败",
										icon: "none"
									});
								}
							});
						}
					}
				})
			},
			save() {
				let that = this
				// #ifdef MP-WEIXIN
				uni.getSetting({
					success(res) {
						console.log('getsetting', res);
						// 是否第一次授权
						if (res.authSetting['scope.writePhotosAlbum'] == undefined) {
							that.downloadFile()
						} else {
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success(res) {
									uni.showLoading({
										title:"保存图片中"
									})
									that.downloadFile()
								},
								fail(err) {
									uni.showModal({
										title: '温馨提示',
										content: '需要授权保存图片或视频到你的相册才能正常使用该功能',
										success(res) {
											if (res.confirm) {
												uni.openSetting({
													success(res) {
														uni.showLoading({
															title:"保存图片中"
														})
														console.log('openSetting', res);
													},
													fail(err) {
														console.log(err, 'openSetting失败');
													}
												})
											}
										}
									})
								}
							})
						}
					}
				})
				//  #endif
				// #ifdef APP-PLUS 
				this.downloadFile()
				//  #endif
				// #ifdef H5
				let urls = this.list.map(item => item.image)
				uni.previewImage({
					current: this.current,
					urls,
					success: res => {
						console.log(res);
					},
					fail: err => {
						console.log(err);
					}
				})
				//  #endif
			},
			send() {
				uni.share({
					provider: "weixin",
					scene: "WXSceneSession",
					type: 5,
					miniProgram: {
					        id: 'gh_7332f30f32d0',
					        path: 'pages/tabbar/index/index',
					        type: 0,
					        webUrl: 'http://uniapp.dcloud.io'
					},
					// href: this.$_BASE_IMG_API + '/dist/pages/login/register/register?tjname=' + this.userInfo
					// 	.username,
					title: "共产星球分享",
					summary: "我正在使用共产星球购物，赶紧跟我一起来体验！",
					imageUrl: "https://ssjh.jifenma.com/assets/img/logo_gxxq.png",
					success: function(res) {
						uni.hideLoading()
						console.log("success:" + JSON.stringify(res));
					},
					fail: function(err) {
						uni.hideLoading()
						console.log("fail:" + JSON.stringify(err));
					}
				});
			},
			share() {
				// console.log("这个是userInfo.username",this.userInfo.username);
			},
			onShareTimeline(res) {
				return {
					title: '共产星球分享',
					path: '/pages/home/index',不支持自定义页面路径
					// query:id=1,//自定义页面路径中携带的参数，如 path?a=1&b=2 的 “?” 后面部分
					// imageUrl: 'https://ssjh.jifenma.com/assets/img/logo_gxxq.png',
				}
			},
			// 自定义此页面的转发给好友(已经有全局的分享方法，此处会覆盖全局)
			onShareAppMessage(res) {
				return {
					title: '共产星球分享',
					path: `/pages/login/login/loginWeixin?id=${this.userInfo.username}`,
					imageUrl: "https://ssjh.jifenma.com/assets/img/logo_gxxq.png",
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page,
	.page {
		min-height: 100vh;
		background-color: #670000;
	}
</style>